Markets

Modified on 2012/01/23 11:04 by Andrew Busby (CTS) — Categorized as: Uncategorized

The Markets property on the MarketData object contains all the markets that have been loaded. It does not contain all the available markets.

The Market object provides the details of a single market, and allows quotes to be obtained. The properties and methods are described here.

To obtain quotes for a market you must subscribe to it using the DepthSubscribe method:

' Subscribe to the market.
oMarket.DepthSubscribe(DepthBuffer.Smart, DepthLevels.Normal)

The parameters are as follows:

BufferThe buffering that should be applied. Smart is the default and is the recommended buffering setting: DepthBuffer
LevelsThe amount of depth that is required. Normal is the default and provides 10 lines of depth: DepthLevels

In order to maintain the correct subscription level then you must handle the MarketCheckSubscription event. If you unsubscribe from a market, or if the API itself changes the subscription level when calculating P&L, then the market will raise a MarketCheckSubscription event to check to see if there is anything else in your application that still needs the subscription. An example of how to handle this event is shown below:

' Ensure that we remain subscribed.
Private Sub moMarket_MarketCheckSubscription(ByVal poMarket As Market, ByRef penDepthBuffer As DepthBuffer, ByRef penDepthLevels As DepthLevels) Handles moMarket.MarketCheckSubscription

penDepthBuffer = poMarket.DepthSubscribeAtLeast(DepthBuffer.Smart, penDepthBuffer)
penDepthLevels = poMarket.DepthSubscribeAtLeast(DepthLevels.All, penDepthLevels)

End Sub

All the handler needs to do is to set the DepthBuffer and DepthLevels parameters to the minimum setting you need, or to the setting passed. You can use the DepthSubscribeAtLeast method to achieve this by passing the level you need and the level passed and it will return the highest of the two values.

NOTE: if you do not implement this event handler correctly then your market subscriptions will stop whenever your position becomes flat.

This allows you to use the same market in several places in your application and have each place take care of its own subscription. Then when the market subscription is no longer needed by any part of your application the API will automatically unsubscribe.

When new market data is received then events are raised. These events are raised directly from the Market object itself, from MarketList objects that the market is contained in and from the MarketData object as well. Which is best for you use to pick up the events depends on how you are structuring your application.

Changes to the depth of market (the bids and offers) and last trade details are raised in the MarketDepthUpdate event. The changes have been applied to the Market’s LastDepth property.

' Event raised when there is a new depth update for the market.
Private Sub moFilter_MarketDepthUpdate(ByVal poMarket As T4.API.Market) Handles moFilter.MarketDepthUpdate

' Display the trade volume changes.
Dim sText As String
sText = "DepthUpdate: " & poMarket.Description

' Display some of the depth details.
sText = sText & ", LastTrade: " & _
poMarket.LastDepth.LastTradeVolume & "@" & _
poMarket.ConvertTicksDisplay( _
poMarket.LastDepth.LastTradeTicks)

' Display the data.
Trace.WriteLine(sText)

End Sub

The LastDepth property values are described here.

If you want to pick up trades occurring then you should handle the MarketDepthTrade event which may be raised for each trade reported by the exchange depending on the DepthBuffer that you have subscribed to.

Changes to the High Low prices for the market are raised with the MarketHighLow event. The changes have been applied to the market objects LastHighLow property:

' Event raised when there is a new high low for the market.
Private Sub moFilter_MarketHighLow(ByVal poMarket As T4.API.Market) Handles moFilter.MarketHighLow

' Display the high and low price.
Trace.WriteLine("HighLow: " & poMarket.Description & _
", " & poMarket.ConvertTicksDisplay( _
poMarket.LastHighLow.HighTicks) & " - " & _
poMarket.ConvertTicksDisplay( _
poMarket.LastHighLow.LowTicks))

End Sub

The LastHighLow property values are described here.

Changes to the Settlement prices for the market are raised with the MarketSettlement event. The changes have been applied to the market objects LastSettlement property.

' Event raised when there is a new settlement price for the Market.
Private Sub moFilter_MarketSettlement(ByVal poMarket As T4.API.Market) Handles moFilter.MarketSettlement

' Display the settlement price.
Trace.WriteLine("Settlement: " & poMarket.Description & _
", " & poMarket.ConvertTicksDisplay( _
poMarket.LastSettlement.Ticks))

End Sub

The LastSettlement property values are described here.

Changes to the Trade Volume data for the market are raised with the MarketTradeVolume event. The changes are passed with the event, and they have also been applied to the market objects LastTradeVolume property.

' Event raised when there are new total trade volume data for the market.
Private Sub moFilter_MarketTradeVolume(ByVal poMarket As T4.API.Market, ByVal poChanges As T4.API.Market.TradeVolume) Handles moFilter.MarketTradeVolume

' Display the trade volume changes.
Dim sText As String
sText = "TradeVolume: " & poMarket.Description

' Loop through each trade volume and add it to the display text.
Dim oVolume As Market.TradeVolume.VolumeItem
For i As Integer = 0 To poChanges.Count - 1

oVolume = poChanges(i)
sText = sText & ", " & _
CStr(oVolume.Volume) & "@" & _
poMarket.ConvertTicksDisplay(oVolume.Ticks)

Next

' Display the data.
Trace.WriteLine(sText)

End Sub

The LastTradeVolume and poChanges parameter are lists of the total volume that has traded at each price today. They also include a Time property which gives the last time that that price traded. These updates are sent out on a slower interval than depth updates (regardless of the DepthBuffer setting). The list also contains a Complete property which determines whether the list contains the entire collection of trade volume data for the day, or just part of it. This is useful when processing the poChanges parameter to check to see if the list has been cleared out due to a trading day change (in which case Complete = True and the list may be empty).